package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.BrandMapper;
import com.atguigu.spzx.manager.mapper.CategoryBrandMapper;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.model.pojo.product.CategoryBrand;
import com.atguigu.spzx.model.request.product.CategoryBrandRequest;
import com.atguigu.spzx.model.vo.product.CategoryBrandVo;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Autowired
    CategoryBrandMapper categoryBrandMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public Page<CategoryBrandVo> findPage(Page<CategoryBrand> page, CategoryBrandRequest categoryBrandRequest) {
        Long brandId = categoryBrandRequest.getBrandId();
        Long id = null;
        if (categoryBrandRequest.getCategory1Id() != null) {
            id = categoryBrandRequest.getCategory1Id();
        }
        if (categoryBrandRequest.getCategory2Id() != null) {
            id = categoryBrandRequest.getCategory2Id();
        }
        if (categoryBrandRequest.getCategory3Id() != null) {
            id = categoryBrandRequest.getCategory3Id();
        }
        Page<CategoryBrand> categoryBrandPage = categoryBrandMapper.findPage(page, brandId, id);
        List<CategoryBrand> records = categoryBrandPage.getRecords();
        Page<CategoryBrandVo> categoryBrandVoPage = new Page<>(categoryBrandPage.getPages(), categoryBrandPage.getSize(), categoryBrandPage.getTotal());
        categoryBrandVoPage.setRecords(new ArrayList<>());
        for (CategoryBrand categoryBrand : records) {
            CategoryBrandVo categoryBrandVo = new CategoryBrandVo();
            categoryBrandVo.setBrandName(brandMapper.selectById(categoryBrand.getBrandId()).getName());
            categoryBrandVo.setCategoryName(categoryMapper.selectById(categoryBrand.getCategoryId()).getName());
            categoryBrandVo.setBrandUrl(brandMapper.selectById(categoryBrand.getBrandId()).getLogo());
            categoryBrandVo.setId(categoryBrand.getId());
            categoryBrandVoPage.getRecords().add(categoryBrandVo);
        }
        return categoryBrandVoPage;
    }

    @Override
    public boolean addCategoryBrand(CategoryBrandRequest categoryBrandRequest) {
        Long brandId = categoryBrandRequest.getBrandId();
        Long id = null;
        if (categoryBrandRequest.getCategory1Id() != null) {
            id = categoryBrandRequest.getCategory1Id();
        }
        if (categoryBrandRequest.getCategory2Id() != null) {
            id = categoryBrandRequest.getCategory2Id();
        }
        if (categoryBrandRequest.getCategory3Id() != null) {
            id = categoryBrandRequest.getCategory3Id();
        }
        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setBrandId(brandId);
        categoryBrand.setCategoryId(id);
        int insert = categoryBrandMapper.insert(categoryBrand);
        DYLAssert.isTrue(insert == 1, "添加失败");
        return true;
    }

    @Override
    public boolean updateCategoryBrand(CategoryBrandRequest categoryBrandRequest, Long id2) {
        Long brandId = categoryBrandRequest.getBrandId();
        Long id = null;
        if (categoryBrandRequest.getCategory1Id() != null) {
            id = categoryBrandRequest.getCategory1Id();
        }
        if (categoryBrandRequest.getCategory2Id() != null) {
            id = categoryBrandRequest.getCategory2Id();
        }
        if (categoryBrandRequest.getCategory3Id() != null) {
            id = categoryBrandRequest.getCategory3Id();
        }
        CategoryBrand categoryBrand = new CategoryBrand();
        categoryBrand.setBrandId(brandId);
        categoryBrand.setCategoryId(id);
        categoryBrand.setId(id2);
        int updateById = categoryBrandMapper.updateById(categoryBrand);
        DYLAssert.isTrue(updateById == 1, "修改失败");
        return true;
    }
}
